﻿using System.Collections;
using UnityEngine;
using Zenject;

public class SceneLifeWaveBattle : SceneLife
{
    [Inject] private WaveManager _waveManager;
    [Inject] private GameManager _gameManager;
    [Inject] private DiContainer _container;
    [Inject(Id = "enemy1Prefab")] private GameObject enemy1Prefab;
    [Inject] private Sprite map;

    /**
     * 场景开始的前处理
     */
    public override void Start()
    {
        base.Start();
        // 开启波次
        _waveManager.WaveIncrease();
        // 倒计时
        StartCoroutine(_waveManager.StartWave());
        // 生成玩家 放置在ProjectContext
        _gameManager.currentPlayer =
            _container.InstantiatePrefab(Resources.Load<GameObject>(_gameManager.currentRoleBaseData.prefab));
        _gameManager.currentPlayer.transform.position = new Vector3(0, 0, 0);
        _gameManager.currentPlayer.GetComponent<PlayerBase>().Init();
        SceneCameraManager.Instance.Follow(_gameManager.currentPlayer.transform);
        // 生成敌人
        StartCoroutine(EnemyGeneration());
    }

    public IEnumerator EnemyGeneration()
    {
        while (true)
        {
            if (_gameManager.currentPlayer.GetComponent<PlayerHealth>().isDead)
            {
                break;
            }

            GameObject enemy = _container.InstantiatePrefab(enemy1Prefab);
            enemy.transform.position = GetRandomPosition(new Bounds(Vector3.zero, map.bounds.size));
            yield return new WaitForSeconds(1);
        }
    }

    private Vector3 GetRandomPosition(Bounds bounds)
    {
        float x = Random.Range(bounds.min.x, bounds.max.x);
        float y = Random.Range(bounds.min.y, bounds.max.y);
        return new Vector3(x, y, 0);
    }
}